package cn.goktech.sports.modules.contentManage.service.impl;

import cn.goktech.sports.common.entity.Page;
import cn.goktech.sports.common.entity.Query;
import cn.goktech.sports.common.entity.R;
import cn.goktech.sports.common.utils.CommonUtils;
import cn.goktech.sports.modules.contentManage.dao.BusinessTypeMapper;
import cn.goktech.sports.modules.contentManage.dao.MessageMapper;
import cn.goktech.sports.modules.contentManage.entity.BusinessTypeEntity;
import cn.goktech.sports.modules.contentManage.entity.MessageEntity;
import cn.goktech.sports.modules.contentManage.service.BusinessTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 业务类型表
 *
 * @author duanyu<>
 */
@Service("businessTypeService")
public class BusinessTypeServiceImpl implements BusinessTypeService {

	@Autowired
	private BusinessTypeMapper businessTypeMapper;

	@Autowired
	private MessageMapper messageMapper;

	/**
	 * 分页查询
	 *
	 * @param params
	 * @return
	 */
	@Override
	public Page<BusinessTypeEntity> listBusinessType(Map<String, Object> params) {
		Query query = new Query(params);
		Page<BusinessTypeEntity> page = new Page<>(query);
		businessTypeMapper.listForPage(page, query);
		return page;
	}

	/**
	 * 新增
	 *
	 * @param businessType
	 * @return
	 */
	@Override
	public R saveBusinessType(BusinessTypeEntity businessType) {
		businessType.setCreateTime(new Date());
		businessType.setDelFlag("0");
		businessType.setStatus("0");
		int count = businessTypeMapper.save(businessType);
		return CommonUtils.msg(count);
	}

	/**
	 * 根据id查询
	 *
	 * @param id
	 * @return
	 */
	@Override
	public R getBusinessTypeById(Long id) {
		BusinessTypeEntity businessType = businessTypeMapper.getObjectById(id);
		return CommonUtils.msg(businessType);
	}

	/**
	 * 修改
	 *
	 * @param businessType
	 * @return
	 */
	@Override
	public R updateBusinessType(BusinessTypeEntity businessType) {
		businessType.setUpdateTime(new Date());
		int count = businessTypeMapper.update(businessType);
		return CommonUtils.msg(count);
	}

	/**
	 * 删除
	 *
	 * @param ids
	 * @return
	 */
	@Override
	public R batchRemove(Long[] ids) {
		for (Long id : ids) {
			List<MessageEntity> messageEntities = messageMapper.listByBusinessId(id);
			if (!Objects.isNull(messageEntities) || !messageEntities.isEmpty()) {
				return R.error("该业务类型下有消息内容，不能删除");
			}
		}
		int count = businessTypeMapper.batchRemove(ids);
		return CommonUtils.msg(ids, count);
	}

	@Override
	public BusinessTypeEntity getByContentId(BusinessTypeEntity entity) {
		return businessTypeMapper.getByContentId(entity);
	}

	@Override
	public R listByContentId(Long contentId) {
		List<BusinessTypeEntity> list = businessTypeMapper.listByContentId(contentId);
		for (BusinessTypeEntity entity : list) {
			List<MessageEntity> messageEntities = messageMapper.listByBusinessId(entity.getBusinessId());
			entity.setList(messageEntities);
			continue;
		}
		return CommonUtils.msg(list);
	}
}
